package com.dcivision.framework.taglib.channel;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Stack;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;
import javax.servlet.jsp.tagext.BodyContent;

import com.dcivision.framework.Utility;

public class AjaxPageContext extends PageContext {

  ServletContext servletContext = null;
  ServletConfig  servletConfig = null;
  HttpServletRequest request = null;
  HttpServletResponse response = null;
  HttpSession session = null;
  JspWriter out = null;
  AjaxBodyContent bodyContent = null;
  Stack bodyContentStack = new Stack();
  
  public AjaxPageContext(){
    out = new AjaxWriter(0,false);
    bodyContent = new AjaxBodyContent(out);
    bodyContentStack.push(bodyContent);
  }
  public Object findAttribute(String arg0) {

    return null;
  }

  public void forward(String arg0) throws ServletException, IOException {


  }

  public Object getAttribute(String arg0) {

    return null;
  }

  public Object getAttribute(String arg0, int arg1) {

    return null;
  }

  public Enumeration getAttributeNamesInScope(int arg0) {

    return null;
  }

  public int getAttributesScope(String arg0) {

    return 0;
  }

  public Exception getException() {

    return null;
  }

  public JspWriter getOut() {

    return out;
  }

  public Object getPage() {

    return null;
  }

  public ServletRequest getRequest() {

    return request;
  }

  public ServletResponse getResponse() {

    return response;
  }

  public ServletConfig getServletConfig() {

    return servletConfig;
  }

  public ServletContext getServletContext() {

    return servletContext;
  }

  public HttpSession getSession() {

    return session;
  }

  public void handlePageException(Exception arg0) throws ServletException,
      IOException {


  }

  public void handlePageException(Throwable arg0) throws ServletException,
      IOException {


  }

  public void include(String arg0) throws ServletException, IOException {


  }

  public void initialize(Servlet arg0, ServletRequest arg1,
      ServletResponse arg2, String arg3, boolean arg4, int arg5, boolean arg6)
      throws IOException, IllegalStateException, IllegalArgumentException {


  }

  public void release() {

     this.request = null;
     this.response = null;
     this.servletConfig = null;
     this.servletContext = null;
     this.session = null;
  }

  public void removeAttribute(String arg0) {


  }

  public void removeAttribute(String arg0, int arg1) {


  }

  public void setAttribute(String arg0, Object arg1) {


  }

  public void setAttribute(String arg0, Object arg1, int arg2) {


  }

  public void setRequest(HttpServletRequest request) {
    this.request = request;
  }

  public void setResponse(HttpServletResponse response) {
    this.response = response;
  }

  public void setServletConfig(ServletConfig servletConfig) {
    this.servletConfig = servletConfig;
  }

  public void setServletContext(ServletContext servletContext) {
    this.servletContext = servletContext;
  }

  public void setSession(HttpSession session) {
    this.session = session;
  }

  public BodyContent pushBody(){
    out = new AjaxWriter(0,false);//need new writer
    AjaxBodyContent bodyContent = new AjaxBodyContent(out);
    bodyContentStack.push(bodyContent);
    return bodyContent;
  }

  public JspWriter popBody(){
    bodyContentStack.pop();
    AjaxBodyContent bodyContent = (AjaxBodyContent)bodyContentStack.lastElement();
    if(!Utility.isEmpty(bodyContent)){
      out = bodyContent.getOut();
    }else{
      out = new AjaxWriter(0,false);
    }
    
    return out;
  }


  /*
   * jsp2.0
   * (non-Javadoc)
   * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
   */
  public void include(String arg0, boolean arg1) throws ServletException, IOException {
    throw(new ServletException(this.getClass().getName()));
  }

/*
 * jsp2.0
 * (non-Javadoc)
 * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
 */
  public ExpressionEvaluator getExpressionEvaluator() {
    return null;
  }

/*
 * jsp2.0
 * (non-Javadoc)
 * @see javax.servlet.jsp.JspContext#getVariableResolver()
 */
  public VariableResolver getVariableResolver() {
    return null;
  }
}
